package com.solr.test;

import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.Reader; 
import java.io.StringReader; 
import java.io.StringWriter; 
import java.io.UnsupportedEncodingException; 
import java.io.Writer; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import org.dom4j.Document; 
import org.dom4j.DocumentException; 
import org.dom4j.io.SAXReader; 

public class PostTool { 
    //发送内容
    public void postData(URL solrUrl, Reader data, Writer output) { 
        HttpURLConnection urlc = null; 
        try { 
            urlc = (HttpURLConnection) solrUrl.openConnection(); 
            try { 
                urlc.setRequestMethod("POST"); 
            } catch (Exception e) { 

            } 
            urlc.setDoOutput(true); 
            urlc.setDoInput(true); 
            urlc.setUseCaches(false); 
            urlc.setAllowUserInteraction(false); 
            urlc.setRequestProperty("Content-type", "text/xml; charset=utf-8"); 

            OutputStream out = urlc.getOutputStream(); 

            try { 
                Writer writer = new OutputStreamWriter(out, "utf-8"); 
                pipe(data, writer); 
                writer.close(); 
            } catch (IOException e) { 

            } finally { 
                if(out!=null) out.close(); 
            } 

            InputStream in = urlc.getInputStream(); 
            
            try { 
                Reader reader = new InputStreamReader(in); 
                pipe(reader, output); 
                reader.close(); 
            } catch (IOException e) { 

            } finally { 
                if(in!=null) in.close(); 
            } 

        } catch (IOException e) { 


        } finally { 
            if(urlc!=null) urlc.disconnect(); 
        } 
    } 
    
    private static void pipe(Reader reader, Writer writer) throws IOException { 
        char[] buf = new char[1024]; 
        int read = 0; 
        while((read = reader.read(buf) ) >= 0) { 
            writer.write(buf, 0, read); 
        } 
        
        writer.flush(); 
    } 
    
    // 查找内容。
    public Document postData(String solrUrl) { 
        Document document = null; 
        try { 
            URL url = new URL(solrUrl); 
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); 
            urlc.setRequestProperty("Content-type", "text/xml; charset=utf-8"); 
            urlc.connect(); 
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "utf-8")); 
            SAXReader reader = new SAXReader(); 
            document = reader.read(in); 
            
            in.close(); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } catch (DocumentException e) { 
            e.printStackTrace(); 
        } 
        
        return document; 
    } 
    
    //用于增加 
    public void postData(String solrUrl, Document document) { 
        try { 
            URL url = new URL(solrUrl); 
            ByteArrayInputStream stream = new ByteArrayInputStream(document.asXML().getBytes("utf-8")); 
            final StringWriter sw = new StringWriter(); 
            postData(url, new InputStreamReader(stream,"utf-8"), sw); 
            postData(url, new StringReader("<commit/>"), sw); 
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } 
    } 
    
} 
